package com.monkeyboy.spring.pattern.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author Gavin
 * @date 2021.03.26 13:25
 */
public class MyCglib {
	public static void main(String[] args)throws Exception {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(A.class); //内部会继承这个类，或者这个类所有的方法等参数然后代理类重写这些方法
		enhancer.setCallback(new MyMethodInterceptor());//调用代理的方法，固定调用这个类的intercept方法
		A cglib =(A)enhancer.create();
		cglib.say();
	}
}
class A{
	public void say(){
		System.out.println("hello cglib.");
	}
}
class MyMethodInterceptor implements MethodInterceptor{
	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		System.out.println("cglib---proxy");
		Object result=methodProxy.invokeSuper(o,null);
		System.out.println("cglib end-----");
		return result;
	}
}
